package pit.com.ordermobilesystem.adapter;

import pit.com.ordermobilesystem.ShopActivity;
import pit.com.ordermobilesystem.R;
import pit.com.ordermobilesystem.adapter.CategoryAdapter.CategoryView;
import pit.com.ordermobilesystem.constant.OrderConstant;
import pit.com.ordermobilesystem.fragment.BaseTabFragment;
import pit.com.ordermobilesystem.fragment.ItemDetailFragment;
import pit.com.ordermobilesystem.fragment.ItemFragment;
import pit.com.ordermobilesystem.imageutil.ImageFetcher;
import pit.com.ordermobilesystem.imageutil.ImageResizer;
import pit.com.ordermobilesystem.provider.BaseProvider;
import pit.com.ordermobilesystem.provider.OrderMobileProvider;
import pit.com.ordermobilesystem.provider.OrderMobileProvider.CategoryTable;
import pit.com.ordermobilesystem.provider.OrderMobileProvider.ItemTable;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ItemAdapter extends CursorAdapter{

	  private LayoutInflater mLayoutInflater;
	  private Context mContext;
	  private ImageResizer mImageWorker;
	public ItemAdapter(Context context, Cursor c) {
		super(context, c,true);
		    mContext = context;
		    mLayoutInflater = LayoutInflater.from(context);
		    mImageWorker = new ImageFetcher(context, OrderConstant.WIDTH_THUMB, OrderConstant.HEIGHT_THUMB);
		    mImageWorker.setErrorImage(R.drawable.ic_loading);
		    mImageWorker.setLoadingImage(R.drawable.ic_loading);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		ItemView item_view=(ItemView) view;
		final ContentValues value=BaseProvider.getContentValues(context, cursor);
		final int idItem=value.getAsInteger(ItemTable.ID_ITEM);
		final ContentValues updateValue=new ContentValues();
		updateValue.put(ItemTable.NUMBER_ORDER, value.getAsInteger(ItemTable.NUMBER_ORDER)+1);
		final String name_item=cursor.getString(cursor.getColumnIndex(ItemTable.NAME_ITEM));
		item_view.tv_title.setText(cursor.getString(cursor.getColumnIndex(ItemTable.NAME_ITEM)));
		String img_item=cursor.getString(cursor.getColumnIndex(ItemTable.LINK_IMAGE));
		if(img_item!=null && !img_item.equals("")){

			mImageWorker.loadImage(img_item,item_view.img_item);
		}else{
			item_view.img_item.setImageResource(R.drawable.ic_apple);	
		}
		if(value.getAsInteger(ItemTable.NUMBER_ORDER)==0){
			item_view.lay_image.setVisibility(View.GONE);
		}else{
			item_view.lay_image.setVisibility(View.VISIBLE);
			item_view.tv_number.setText(value.getAsString(ItemTable.NUMBER_ORDER));
		}
		item_view.tv_description.setText(cursor.getString(cursor.getColumnIndex(ItemTable.DESCRIPTION)));
		item_view.tv_price.setText(cursor.getString(cursor.getColumnIndex(ItemTable.PRICE))+context.getString(R.string.d));
		item_view.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				ShopActivity activty=(ShopActivity) mContext;
				ItemDetailFragment fragment=new ItemDetailFragment();
				 Bundle bundle=new Bundle();
				 bundle.putInt("item_id", idItem);
				 bundle.putString("item_name", name_item);				
				fragment.setArguments(bundle);
				activty.addFragment(fragment);
			

			}
		});
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		return new ItemView(context);
	}
	
	class ItemView extends RelativeLayout {
		TextView tv_title;
		ImageView img_item;
		TextView tv_price;
		TextView tv_description;
		TextView tv_number;
		RelativeLayout lay_image;
		public ItemView(Context context) {
			super(context);
			LayoutInflater li = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			li.inflate(R.layout.view_item, this, true);
			tv_title = (TextView) findViewById(R.id.tv_title);
			img_item = (ImageView) findViewById(R.id.img_item);
			tv_price = (TextView) findViewById(R.id.tv_price);

			tv_description = (TextView) findViewById(R.id.tv_description);
			tv_number=(TextView) findViewById(R.id.tv_number);
			lay_image=(RelativeLayout) findViewById(R.id.lay_image);
		}
	}	
	
	

}
